package dapps.web.proj;

import javax.servlet.ServletContext;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.web.struts2.CRUDActionSupport;
import org.springside.modules.web.struts2.Struts2Utils;

import dapps.entity.proj.Proj;
import dapps.entity.proj.Type;
import dapps.entity.user.User;
import dapps.service.ServiceException;
import dapps.service.proj.ProjManager;


@SuppressWarnings("serial")
@Results( { 
	@Result(name = CRUDActionSupport.RELOAD, location = "applicate.action?page.pageRequest=${page.pageRequest}&pagely.pageRequest=${pagely.pageRequest}", type = "redirect")
	})
public class ApplicateAction extends CRUDActionSupport<Proj> {

	@Autowired
	private ProjManager projManager;

	// 基本属性
	private Proj entity;
	private Long id;	
	private Long oldid;	
	private Long typeid;
	private Long dutycompid;
	private Page<Proj> page = new Page<Proj>(8);//每页5条记录
	private Page<Proj> pagely = new Page<Proj>(8);
	private ServletContext application = Struts2Utils.getApplication();
	private Integer[] ps  = (Integer[])application.getAttribute("planarray");
	private Integer anew=0;//结转
	private Proj oldentity;
	// 角色相关属性

	// 基本属性访问函数 //

	public Proj getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		

		if (id != null&&anew==0) {
			entity = projManager.get(id);
			if(entity.getType()!=null) typeid = entity.getType().getId();
			if(entity.getDutycomp()!=null) dutycompid = entity.getDutycomp().getId();			
		} else {
			entity = new Proj();
			if(oldid != null&&anew==1){
				oldentity = projManager.get(oldid);
				PropertyUtils.copyProperties(entity, oldentity);				
				entity.setType(oldentity.getType());
				entity.setId(null);
				typeid = entity.getType().getId();
			}
			if(ps[0]!=0){
				entity.setYeartime(ps[0]);
				entity.setApplicate(ps[0]*10);
			}			
			entity.setDutycomp(SpringSecurityUtils.getCurrentUser());
			entity.setApplicateuser(SpringSecurityUtils.getCurrentUser());
			dutycompid = SpringSecurityUtils.getCurrentUser().getId();
		}

	}

	public void setId(Long id) {
		this.id = id;
	}
	public void setTypeid(Long typeid) {
		this.typeid = typeid;
	}
	public Page<Proj> getPage() {
		return page;
	}

	
	public Page<Proj> getPagely() {
		return pagely;
	}

	public Long getTypeid() {
		return typeid;
	}

// CRUD Action 函数 //
	@Override
	public String list() throws Exception {
		Criterion[] criterions = {Restrictions.eq("yeartime", ps[0])
				,Restrictions.or(Restrictions.eq("applicateuser.id",SpringSecurityUtils.getCurrentUser().getId())
								,Restrictions.eq("dutycomp.id",SpringSecurityUtils.getCurrentUser().getId()))
				,Restrictions.eq("applicate",ps[0]*10)
				,Restrictions.ne("prorank",9)};		

		String[] alias={"type","t"};
		page.setOrderBy("insertDate");page.setOrder("desc");
		page = projManager.findByCriteria(page,null, criterions, alias);
		
		
		Criterion[] criterionsly = {
				Restrictions.eq("dutycomp.id", SpringSecurityUtils.getCurrentUser().getId())
				,Restrictions.between("prorank", 1,3)
				,Property.forName("yeartime").between(ps[0]-100,ps[0]-1)				
				,Restrictions.eq("jzto",0l)
				};//去年非实施结转
		pagely.setOrderBy("prorank,id");pagely.setOrder("asc,asc");
		pagely = projManager.SearchCriterion(pagely,criterionsly);
		
		return SUCCESS;
	}
	
	
	@Override
	public String input() throws Exception {				
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		if(typeid!=null){
			Type type = new Type();
			type.setId(typeid);
			entity.setType(type);
		}
		if(dutycompid!=null&&dutycompid>0){
			User u = new User();
			u.setId(dutycompid);
			entity.setDutycomp(u);
		}
		if(entity.getSl()==0&&entity.getForced()==2) entity.setForced(0);//forced==2 means sl==1
		if(entity.getSl()==1){
			entity.setProrank(3);
			entity.setForced(2);
		}
		if (entity.getForced() == 0) {
			if (projManager.isSs(entity)) {
				entity.setProrank(0);// 实施
			} else if (projManager.isCb(entity)) {
				entity.setProrank(2);// 储备
			} else {
				entity.setProrank(1);// 预备
			}
		}
		if(oldid!=null&&oldid>0){
			entity.setJzfrom(oldid);			
		}
		projManager.saveProj(entity);
		if(oldid!=null&&oldid>0){
			oldentity = projManager.get(oldid);
			oldentity.setJzto(entity.getId());
			projManager.saveProj(oldentity);
		}
		addActionMessage("保存申报成功");
		return RELOAD;
	}

	
	@Override
	public String delete() throws Exception {
		try {
			Proj p = projManager.get(id);
			User au = p.getApplicateuser();
			User du = p.getDutycomp();
			Long uid = SpringSecurityUtils.getCurrentUser().getId();
			if(p!=null&&(au.getId().compareTo(uid)==0||du.getId().compareTo(uid)==0)){
				if(p.getJzfrom()!=null&&p.getJzfrom()!=0l){
					Proj pfrom =projManager.get(p.getJzfrom());
					if(pfrom!=null) {
						pfrom.setJzto(0l);
						projManager.save(pfrom);
					}					
				}
				projManager.delete(p);
				addActionMessage("删除项目成功");
			}else addActionMessage("缺少权限");			
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return RELOAD;
	}

	public Long getDutycompid() {
		return dutycompid;
	}

	public void setDutycompid(Long dutycompid) {
		this.dutycompid = dutycompid;
	}

	public Integer getAnew() {
		return anew;
	}

	public void setAnew(Integer anew) {
		this.anew = anew;
	}

	public Proj getOldentity() {
		return oldentity;
	}

	public Long getOldid() {
		return oldid;
	}

	public void setOldid(Long oldid) {
		this.oldid = oldid;
	}


}
